
import re

html = """
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>正则测试
  </title>
</head>
<body>
<h1>正则测试案例练习</h1>
<div>
  <ul>
    <li><a href="https://www.baidu.com/new1">连接1</a></li>
    <li><a href="https://www.baidu.com/new2">连接2</a></li>
    <li><a href="https://www.baidu.com/new3">连接3</a></li>
    <li><a href="https://www.baidu.com/new4">连接4</a></li>
    <li><a href="https://www.baidu.com/new5">连接5</a></li>
  </ul>
</div>

</body>
</html>
"""


pattern = r'<li><a href="(?P<链接>.*?)">(?P<内容>.*?)</a></li>'

# 元组
# print(re.search(pattern,html,re.S))
# print(re.findall(pattern,html,re.S))
# items = re.findall(pattern,html,re.S)
# for item in items:
    # print(f'链接:{item[0]} 名字:{item[1]}')

# 字典
items = re.finditer(pattern,html,re.S)
for item in items:
    print(f'链接:{item.group("链接")} 链接:{item.group("内容")}')

